<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\Db;
use think\Session;

// 应用公共文件
//封装密码的加密文件
if (!function_exists('encrypt_password')) {
    //定义密码的加密函数
    function encrypt_password($password, $salt = '')
    {
        if (!$salt) {
            $salt = mt_rand(10000, 99999);
        }
        $password = md5(md5($password) . $salt);
        return array('salt' => $salt, 'password' => $password);
    }
}
// 生成随机字符串
if (!function_exists('generatePassword')) {
    function generatePassword($length = 6)
    {
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $password = "";
        for ($i = 0; $i < $length; $i++) {
            $password .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $password;
    }
}
//获取用户的ip
if (!function_exists('getIp')) {
    function getIP()
    {
        static $ip = NULL;
        if ($ip !== NULL) return $ip;

        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos = array_search('unknown', $arr);
            if (false !== $pos) unset($arr[$pos]);
            $ip = trim($arr[0]);
        } else if (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } else if (isset($_SERVER['REMOTE_ADDR'])) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }

        //IP地址合法验证
        $ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0';
        return $ip;
    }
}
//删除文件夹及其下面的所有文件
if (!function_exists('delFile')) {
    function delFile($dir)
    {
        $dh = opendir($dir);//打开文件夹
        while ($file = readdir($dh)) {
            if ($file != "." && $file != "..") {
                $fullpath = $dir . "/" . $file;
                if (is_dir($fullpath)) {
                    delFile($fullpath);
                } elseif (is_file($fullpath) && isset($fullpath)) {
                    unlink($fullpath);
                }
            }
        }
        closedir($dh);
        rmdir($dir);
    }
}
//匹配用户名
if (!function_exists('pregUser')) {
    //匹配用户名
    function pregUser($text, $min = 1, $max = 8)
    {
        $rule = '/^[a-zA-Z\x{4e00}-\x{9fa5}0-9_]{' . $min . ',' . $max . '}$/u';
        preg_match($rule, $text, $result);
        return $result;
    }
}
//匹配手机号码
if (!function_exists('pregPN')) {
    function pregPN($text)
    {
        $rule = "/^1\d{10}$/";
        preg_match($rule, $text, $result);
        return $result;
    }
}
//匹配邮箱
if (!function_exists('pregMAIL')) {
    function pregMAIL($text)
    {
        $rule = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/";
        preg_match($rule, $text, $result);
        return $result;
    }
}
//匹配身份证
if (!function_exists('precard')) {
    function precard($card)
    {
        $rule = "/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/";
        preg_match($rule, $card, $result);
        return $result;
    }
}
//验证密码
if (!function_exists('pregPW')) {
    function pregPW($text, $min = 6, $max = 32)
    {
        $rule = '/^[a-zA-Z0-9_]{' . $min . ',' . $max . '}$/';
        preg_match($rule, $text, $result);
        return $result;
    }
}
//封装一个无限极分类
if (!function_exists('getTree')) {
    //递归方法实现无限极分类
    function getTree($list, $pid = 0, $level = 0)
    {
        static $tree = array();
        foreach ($list as $row) {
            if ($row['pid'] == $pid) {
                $row['level'] = $level;
                $tree[] = $row;
                getTree($list, $row['id'], $level + 1);
            }
        }
        return $tree;
    }
}

if (!function_exists('getTrees')) {
    //递归方法实现无限极分类
    function getTrees($list, $pid = 0, $level = 0)
    {
        static $tree = array();
        foreach ($list as $row) {
            if ($row['pid'] == $pid) {
                $row['level'] = $level;
                $tree[] = $row;
                getTrees($list, $row['account_id'], $level + 1);
            }
        }
        return $tree;
    }
}


// 无限分类，查找子级
if (!function_exists('getTreeData')) {
    function getTreeData($table, $field, $where = '1=1', $pid = 0, $data = array())
    {
        $_data = Db::name($table)
            ->field($field)
            ->where($where)
            ->where('pid', $pid)
            ->order('drank asc')
            // ->fetchSql()
            ->select();
        // var_dump($data);die;
        if (!empty($_data)) {
            foreach ($_data as $k => $v) {
                $data[$k] = $_data[$k];
                $res = getTreeData($table, $field, $where, $v['id']);
                if (!empty($res)) {
                    $data[$k]['child'] = $res;
                }
                // var_dump($data);die;
            }
            return $data;
        } else {
            return $data;
        }
    }
}


// 无限分类，多表联查
if (!function_exists('getTreeDatas')) {
    function getTreeDatas($table, $join, $field, $where = '1=1', $pid = 0, $data = array())
    {
        $_data = Db::name($table)
            ->alias('zu')
            ->join($join)
            ->field($field)
            ->where($where)
            ->where('zu.pid', $pid)
            ->order('zu.drank asc')
            ->select();
        if (!empty($_data)) {
            foreach ($_data as $k => $v) {
                $data[$k] = $_data[$k];
                $res = getTreeDatas($table, $join, $field, $where, $v['id']);
                if (!empty($res)) {
                    $data[$k]['child'] = $res;
                }
            }
            return $data;
        } else {
            return $data;
        }
    }
}


//无限极分类
if (!function_exists('getFrames')) {
    function getFrames($department = 'department', $lang = null, $country_id = 0)
    {
        //获取当前用户的信息
        $admin = session('admin');
        switch ($department) {
            case 'department':
                $fid = $admin['department_id'];//部门id
                break;
            case 'position':
                $fid = $admin['position_id'];//职位id
                break;
            default:
                $fid = $admin['department_id'];//传过来的id
                break;
        }
        if ($admin['position_id'] == 1) {
            //是管理员
            $uid = 0;
        } else {
            $uid = $fid ? $fid : 0;
        }
        return getInfiniteSort($department, $lang, $country_id, $uid);
    }
}
//获取无限分类
if (!function_exists('getInfiniteSort')) {
    function getInfiniteSort($db_name, $lang, $country_id, $uid = 0, $i = 0)
    {
        $i++;
        $where['pid'] = $uid;
        $where['country_id'] =  ['in', $country_id];
        $rs = Db::name($db_name)->where($where)->select();//获取数据
        if ($lang) {
            //含有语言获取一个
            foreach ($rs as $rskey => $rsval) {
                $rs[$rskey]['langname'] = Db::name('multi_lang')->where(['mkey' => $rsval['mkey'], 'lang' => $lang])->value('content');
            }
        } else {
            //没有获取多个
            foreach ($rs as $rskey => $rsval) {
                $langname = Db::name('multi_lang')->field('lang,content')->where('mkey', $rsval['mkey'])->select();
                foreach ($langname as $langnamekey => $langnameval) {
                    $rs[$rskey][$langnameval['lang'] . 'name'] = $langnameval['content'];
                }
            }
        }
        $return = array();
        foreach ($rs as $value) {
            $value['level'] = $i;
            $return[] = $value;
            foreach (getInfiniteSort($db_name, $lang, $country_id, $value['id'], $i) as $value_2) {
                $return[] = $value_2;
            }
        }
        return $return;
    }
}

// 无限分类子级
if (!function_exists('getTreeChild')) {
    function getTreeChild($table, $where = '1=1', $lang = null, $pid = 0, $data = array())
    {
        $_data = Db::name($table)
            ->where($where)
            ->where('pid', $pid)
            ->order('drank asc')
            ->select();
        if ($lang) {
            //获取一种
            foreach ($_data as $_datakey => $_dataval) {
                $_data[$_datakey]['langname'] = Db::name('multi_lang')->where(['mkey' => $_dataval['mkey'], 'lang' => $lang])->value('content');
            }
        } else {
            //获取多种
            foreach ($_data as $_datakey => $_dataval) {
                $langname = Db::name('multi_lang')->field('lang,content')->where('mkey', $_dataval['mkey'])->select();
                foreach ($langname as $langnamekey => $langnameval) {
                    $_data[$_datakey][$langnameval['lang'] . 'name'] = $langnameval['content'];
                }
            }
        }
        if (!empty($_data)) {
            foreach ($_data as $k => $v) {
                $data[$k] = $_data[$k];
                $res = getTreeChild($table, $where, $lang, $v['id']);
                if (!empty($res)) {
                    $data[$k]['child'] = $res;
                }
            }
            return $data;
        } else {
            return $data;
        }
    }
}


//获取系统的相关配置
if (!function_exists('getConfig')) {
    function getConfig()
    {
        $rs = Db::name('config')->select();
        foreach ($rs as $value) {
            $array[$value['dname']] = $value['dvalue'];
            switch (cookie('think_var')) {
                case 'zh-cn':
                    $array[$value['dname']] = $value['dvalue'];
                    break;
                case 'en-us':
                    $array[$value['dname']] = $value['dvalue_en'];
                    break;
                case 'Portug-us':
                    // $array[$value['dname']] = $value['dvalue_pu'];
                    $array[$value['dname']] = $value['dvalue'];
                    break;
            }
        }
        return $array;
    }
}

if (!function_exists('removeBlank')) {
    function removeBlank($str)
    {
        $qian = array(" ", "　", "", "\t", "\n", "\r");
        $hou = array("", "", "", "", "");
        return str_replace($qian, $hou, $str);
    }
}

/*
导出
$title 文件名
$head 表头
$data 数据
*/
function export($title, $head, $data)
{
//    file_put_contents("test.txt", "标题 = " . $title . "\r\n", FILE_APPEND);
//    file_put_contents("test.txt", "头部 = " . json_encode($head) . "\r\n", FILE_APPEND);
//    file_put_contents("test.txt", "内容 = " . json_encode($data) . "\r\n", FILE_APPEND);
    require_once(getRoot(3) . 'PHPExcel/PHPExcel.php');
    $obj_php_excel = new \PHPExcel(); //创建一个实例
    $obj_php_excel->setActiveSheetIndex(0);
    $obj_act_sheet = $obj_php_excel->getActiveSheet();
    $width = array();
    // 设置表头
    foreach ($head as $key => $value) {
        $line = 1;
        $index = PHPExcel_Cell::stringFromColumnIndex($key);
        $station = $index . $line;
        // 设置列宽
        $width[$key] = strlen($value);
        $obj_act_sheet->getColumnDimension($index)->setWidth($width[$key]);
        // 左右居左
        $obj_act_sheet->getStyle($index)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        // 垂直居左
        $obj_act_sheet->getStyle($index)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        // 设置背景颜色
        $obj_act_sheet->getStyle($station)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
        $obj_act_sheet->getStyle($station)->getFill()->getStartColor()->setARGB('006614');
        // 设置字体颜色
        $obj_act_sheet->getStyle($station)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
        // 设置行高
        $obj_act_sheet->getRowDimension($key)->setRowHeight(18);
        // 设置内容
        $obj_act_sheet->setCellValue($station, $value);
    }
    // 设置内容
    foreach ($data as $key => $value) {
        $line = $key + 2;
        $i = 0;
        foreach ($value as $key_1 => $value_1) {
            $index = PHPExcel_Cell::stringFromColumnIndex($i);
            $station = $index . $line;
            // 设置列宽
            $length = strlen($value_1);
            if ($length > $width[$i]) $width[$i] = $length;
            $obj_act_sheet->getColumnDimension($index)->setWidth($width[$i]);
            // 设置内容
            $obj_act_sheet->setCellValue($station, $value_1);
            $i++;
        }
    }
    $obj_act_sheet->setTitle($title);
    // header("Pragma: public");
    // header("Expires: 0");
    // header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
    // header("Content-Type:application/force-download");
    // header("Content-Type:application/vnd.ms-execl");
    // header("Content-Type:application/octet-stream");
    // header("Content-Type:application/download");
    header('Content-Disposition:attachment;filename="' . $title . '.xls"');
    // header("Content-Transfer-Encoding:binary");

    $obj_writer = new \PHPExcel_Writer_Excel2007($obj_php_excel);
    $obj_writer->save('php://output');
}

//实现用户的操作记录的操作
if (!function_exists('setRecord')) {
    function setRecord($action, $text, $model = null)
    {
        $data['uid'] = session('userid');
        if ($action == 4 || $action == 5) {
            if (strpos($text, '-') != false) { //添加登录失败记录
                $textArr = explode('-', $text);
                $umap['dadmin'] = $textArr[1];
                //根据用户名查找ID
                $uInfo = Db::name('admin')->where($umap)->find();
                if ($uInfo) {
                    $data['uid'] = $uInfo['did'];
                } else {
                    $data['uid'] = 0;
                }
            }
        }
        if ($model) {
            $useInfo = $model;
        } else {
            //根据控制器获取模块
            $map['dckc'] = request()->controller();
            $map['dcka'] = 'index';
            $useInfo = Db::name('menu')->where($map)->value('dname');
        }
        $data['module'] = $useInfo;
        $data['action'] = $action;
        $data['content'] = $text;
        $data['create_time1'] = time();
        Db::name('record')->insert($data);
    }
}

//CURL
if (!function_exists('curlRequest')) {
    function curlRequest($url, $data)
    {
        $ch = curl_init();
        $res = curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = json_decode(curl_exec($ch), true);
        curl_close($ch);
        return $result;
    }
}

//翻译接口
if (!function_exists('TransQuery')) {
    // 百度翻译开放平台 http://api.fanyi.baidu.com/api/trans/product/apidoc
    function TransQuery($queryData, $lang)
    {
        $api_key = 'rsg3m_zW_lO48hTnm2ZL';
        $baiduTransApi = 'https://fanyi-api.baidu.com/api/trans/vip/translate';
        $data = array(
            'q' => $queryData,
            'appid' => '20190912000333880',
            'salt' => rand(10000, 99999),
            'from' => 'auto',
            'to' => $lang,
        );
        $data['sign'] = md5($data['appid'] . $queryData . $data['salt'] . $api_key);
        $returnData = curlRequest($baiduTransApi, $data);
        return json_encode($returnData);
    }
}


// 编辑排序
if (!function_exists('changeDrank')) {
    function changeDrank($table, $where, $drank)
    {
        $res = Db::name($table)->where($where)->where($drank)->select();
        // dump($res);die;
        if (!empty($res)) {
            foreach ($res as $k => $v) {
                $ress = Db::name($table)->where('id', $v['id'])->update(['drank' => ($v['drank'] + 1)]);
            }
            return $ress;
        } else {
            return 1;
        }
    }
}

//编辑数据
if (!function_exists('getinfo')) {
    function getinfo($did, $db_name, $field = '*')
    {
        $res = Db::name($db_name)->where('id', $did)->field($field)->find();
        return $res;
    }
}
//获取对应的语言的信息
if (!function_exists('getlanginfo')) {
    function getlanginfo($db_name, $where)
    {
        $info = Db::name($db_name)->where($where)->find();//编辑的数据
        $mkey = $info['mkey'];
        $langname = Db::name('multi_lang')->where(['mkey' => $mkey])->select();//获取多语言的数据
        foreach ($langname as $langnamekey => $langnameval) {
            $info['lang_' . $langnameval['lang']] = $langnameval['content'];
        }
        return $info;
    }
}
if (!function_exists('getables')) {
    function getables($tab, $where = [], $field = '*', $sortid = '')
    {
        if ($sortid == 'drank')
            $tabInfo = Db::name($tab)->field($field)->where($where)->order($sortid)->select();
        else
            $tabInfo = Db::name($tab)->field($field)->where($where)->select();
        return $tabInfo;
    }
}
//获取对应的语言的内容
if (!function_exists('getlangname')) {
    function getlangname($dbname, $where, $lang)
    {
        $where['lang'] = $lang;
        //获取一种
        $data = Db::name($dbname)->where($where)->select();
        $resdata = implode('/', array_column($data, 'content'));
        return $resdata;
    }
}


//不能为空
if (!function_exists('notEmpty')) {
    function notEmpty($array)
    {
        foreach ($array as $key => $value) {
            if (!$_POST[$key]) {
                $ret['status'] = 2;
                $ret['msg'] = $value . lang("can't_be_empty").'!!!';
                return $ret;
            }
        }
        $ret['status'] = 1;
        return $ret;
    }
}

// 获取当前语言
if (!function_exists('is_lang')) {
    function is_lang($lang_type = '')
    {
        if ($lang_type) {
            switch ($lang_type) {
                case 'zh':
                    cookie('think_var', 'zh-cn');
                    break;
                case 'en':
                    cookie('think_var', 'en-us');
                    break;
                case 'pt':
                    cookie('think_var', 'pt-br');
                    break;
            }
        }
        // 获取当前语言.
        switch (\think\Lang::detect()) {
            case 'zh-zn':
                return 'zh';
                break;
            case 'en-us':
                return 'en';
                break;
            case 'pt-br':
                return 'pt';
                break;
            default:
                return 'zh';
                break;
        }

    }
}

if (!function_exists('getLangData')) {
    function getLangData($langText, $abbreviation, $lang_type)
    {
        switch ($lang_type) {
            case 'zh':
                $data['lang_en'] = $langText . '_en';
                $data['lang_pt'] = $langText . '_pt';
                break;

            case 'en':
                $data['lang_zh'] = $langText . '_zh';
                $data['lang_pt'] = $langText . '_pt';
                break;

            case 'pt':
                $data['lang_zh'] = $langText . '_zh';
                $data['lang_en'] = $langText . '_en';
                break;
        }
        $data['abbreviation'] = $abbreviation;
        return $data;
    }
}
// 判断是否已经存在
if (!function_exists('isIsset')) {
    function isIsset($table, $swhereKey, $whereValue, $where = '1=1')
    {
        $res = Db::name($table)->where($swhereKey, $whereValue)->where($where)->find();
        if ($res) {
            return $res;
        } else {
            return false;
        }
    }
}

if (!function_exists('getTimeDates')) {
    function getTimeDates($time, $format = 0, $lang)
    {
        switch ($lang) {
            case 'zh':
                return date('Y-m-d H:i:s', $time);
                break;
            default:
                return date('d-m-Y H:i:s', $time);
                break;
        }
    }

}


if (!function_exists('getTimeDate')) {
    function getTimeDate($time, $format = 0)
    {
        if (!$time) $time = time();
        switch ($format) {
            case 1:
                return date('Y', $time);
                break;
            case 2:
                return date('m', $time);
                break;
            case 3:
                return date('d', $time);
                break;
            case 4:
                return date('H:i:s', $time);
                break;
            case 5:
                return date('H', $time);
                break;
            case 6:
                return date('W', $time);
                break;
            case 7:
                return date('Y-m-d', $time);
                break;
            case 8:
                return date('d-m-Y', $time);
                break;
            case 9:
                return date('d-m-y H:i:s', $time);
                break;
            case 10:
                return date('Y-m', $time);
                break;
            case 11:
                return date('m-Y', $time);
                break;
            case 12:
                return date('y-m-d H:i:s', $time);
                break;
            case 13:
                return date('m-d-Y', $time);
                break;
            default:
                return date('Y-m-d H:i:s', $time);
                break;
        }
    }
}

//获取下一个月的日期
if (!function_exists('getNextMonthDays')) {
    function getNextMonthDays($date)
    {
        $firstday = date('Y-m-01', strtotime($date));
        $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
        return array($firstday, $lastday);
    }
}

if (!function_exists('getLanguage')) {
    function getLanguage($lang = '')
    {
        $res = Db::name('language')->select();
        foreach ($res as $k => $v) {
            if ($v['code'] == $lang) {
                unset($res[$k]);
                array_unshift($res, $v);
            }
        }
        return $res;
    }
}

if (!function_exists('getNoticLang')) {
    function getNoticLang($lang)
    {
        switch ($lang) {
            case 'zh':
                return '中文';
                break;

            case 'en':
                return 'English';
                return;

            case 'pt':
                return 'Portuguese';
                return;
        }
    }
}
// group 搜索
if (!function_exists('getGroup')) {
    function getGroup($table, $field, $where, $group)
    {
        $res = Db::name($table)->field($field)->where($where)->group($group)->select();
        return $res;
    }
}

// 联表查询
if (!function_exists('getSearch')) {
    function getSearch($table, $alias, $field, $where = "1=1", $join, $order,$whereOr=[],$group='',$limit='')
    {   
        // $group = $alias.'.id';
        $res = Db::name($table)
            ->alias($alias)
            ->join($join)
            ->field($field)
            ->where($where)
            ->whereOr($whereOr)
            ->order($order)
            ->group($group)
            ->limit($limit)
            // ->fetchSql()
            ->select();
        return $res;
    }
}


// 按多语言类型获取页面文本语言格式
if (!function_exists('getViewMultiLanguage')) {
    function getViewMultiLanguage($switch, $lang = 'zh')
    {
        switch ($switch) {
            case '1':
                $res = 'recovery';    // 正常
                break;
            case '2':
                $res = 'delete';    // 软删除
                break;
            case '3':
                $res = 'hide';      // 隐藏
                break;
            case '4':
                $res = 'off_the_shelf';  // 下架
                break;
            case '5':
                $res = 'clear';  // 清除
                break;
            default:
                $res = $switch;
                break;
        }
        return $res;
    }
}

// 获取单个字段值
if (!function_exists('getValue')) {
    function getValue($table, $where, $value)
    {
        return Db::name($table)->where($where)->value($value);
    }
}

// 新增数据
if (!function_exists('insertData')) {
    function insertData($table, $insertData)
    {
        $res = Db::name($table)->insertGetId($insertData);
        return $res;
    }
}

// 修改数据
if (!function_exists('updateData')) {
    function updateData($table, $where, $updateData)
    {
        $res = Db::name($table)->where($where)->update($updateData);
        return $res;
    }
}

// 删除数据
if (!function_exists('deleteData')) {
    function deleteData($table, $where, $idArray = array())
    {
        if (empty($idArray)) {
            $res = Db::name($table)->where($where)->delete();
            return $res;
        } else {
            Db::startTrans();
            try {
                foreach ($idArray as $k => $v) {
                    $res = Db::name($table)->where('id', $v)->delete();
                }
                if ($res >= 0) {
                    Db::commit();
                    return array('code' => 2000, 'msg' => lang($table . '__operational_success'));
                } else {
                    return array('code' => 500, 'msg' => lang($table . '__operational_fail'));
                }
            } catch (\Exception $e) {
                Db::rollback();
                return array('code' => 500, 'msg' => $e);
            }
        }
    }
}

// 是否有数据
if (!function_exists('hasData')) {
    function hasData($table, $where)
    {
        $res = Db::name($table)->where($where)->count();
        return $res;
    }
}

// 获取数据
if (!function_exists('getDatas')) {
    function getDatas($table, $field, $where, $order = 'id desc')
    {
        $res = Db::name($table)->field($field)->where($where)->order($order)->select();
        return $res;
    }
}


// 获取多语言数据
if (!function_exists('getMultiLangName')) {
    function getMultiLangName($table, $field, $where, $order, $lang = '')
    {
        $resData = getDatas($table, $field, $where, $order);
        if (count($resData) > 1) {
            foreach ($resData as $k => $v) {
                if($table == 'prochar') {
                    $where1['mkey'] = $v['ctn_note'];
                }else{
                    $where1['mkey'] = $v['mkey'];
                }
                $foreachData = getDatas('multi_lang', ['*'], $where1);
                foreach ($foreachData as $k1 => $v1) {
                    $resData[$k]['name_' . $v1['lang']] = $v1['content'];
                }
            }

        } else if (count($resData) == 1) {
            if($table == 'prochar') {
                $where1['mkey'] = $resData[0]['ctn_note'];
            }else{
                $where1['mkey'] = $resData[0]['mkey'];
            }
            $foreachData = getDatas('multi_lang', ['*'], $where1);
            foreach ($foreachData as $k => $v) {
                $resData[0]['name_' . $v['lang']] = $v['content'];
            }
        }
        return $resData;
    }
}


if (!function_exists('getTreeParent')) {
    function getTreeParent($table, $field, $where, $data = array())
    {
        $resData = getMultiLangName($table, $field, $where, 'id asc')[0];
        if ($resData['pid'] > 0) {
            $res = getTreeParent($table, $field, ['id' => $resData['pid']], $data);
            array_push($data, $res[0]);
        }
        array_push($data, $resData);
        return $data;
    }
}

if(!function_exists('noticeText'))
{
    function noticeText($array,$Wherearray,$noticeString='不能为空')
    {
        foreach ($Wherearray as $k => $v) {
            if(!$array[$k]) return array('code'=>500,'msg'=>$v.$noticeString);
        }
    }
}



if (!function_exists('getChildID')) {
    // 获取当前类行下 所以能用的子级
    function getChildID($table, $field, $where, $pid = 0, $data = [])
    {
        $_data = Db::name($table)
            ->field($field)
            ->where($where)
            ->where('pid', $pid)
            // ->fetchSql()
            ->select();
        // return $_data;
        if (!empty($_data)) {
            // array_push($data,$_data);
            foreach ($_data as $k => $v) {
                array_push($data, $v['id']);
                $res = getChildID($table, $field, $where, $v['id'], $data);
                // array_push($data,$res);
            }
        }
        return $data;
    }
}

//获取对应数据库的语言的名称
if (!function_exists('getmultidata')) {
    function getmultidata($table, $join, $where, $pagelimit = null, $order = 'create_at desc')
    {
        if ($pagelimit) {
            $data = Db::name($table)->alias('a')->join($join)->field('b.*,a.content')->where($where)->limit($pagelimit['start'], $pagelimit['limit'])->order($order)->select();
        } else {
            $data = Db::name($table)->alias('a')->join($join)->field('b.*,a.content')->where($where)->order($order)->select();
        }
        $count = Db::name($table)->alias('a')->join($join)->where($where)->count();
        return array('count' => $count, 'data' => $data);
    }
}
//判断是否适用于个人
if (!function_exists('getcountry')) {
    function getcountry($country_id)
    {
        if ($country_id == 0) {
            $status = lang("universal");
        } elseif ($country_id == 1) {
            $status = lang("BRAZIL");
        } elseif ($country_id == 2) {
            $status = lang("CHINA");
        }
        return $status;
    }
}

//处理职位的显示
if (!function_exists('getpostion')) {
    function getpostion($dbname, $id)
    {
        //初始化ID数组
        $array[] = $id;
        do {
            $ids = '';
            $where['id'] = array('in', $id);
            $dbselect = Db::name($dbname)->where($where)->select();
            foreach ($dbselect as $v) {
                $array[] = $v['pid'];
                $ids .= ',' . $v['pid'];
            }
            $ids = substr($ids, 1, strlen($ids));
            $id = $ids;
        } while (!empty($dbselect));
        foreach ($array as $arraykey => $arrayval) {
            if ($arrayval == 0) {
                unset($array[$arraykey]);
            }
        }
        //$ids = implode(',', $array);
        return $array;
    }
}

//联系人和银行的公司的处理
if (!function_exists('linkbankhandle')) {
    function linkbankhandle($co_id)
    {
        //清空公司
        $linkids = implode(",", array_column(Db::name('linkman')->where("co_id in($co_id)")->select(), 'id'));//获取这个公司下的所有的联系人id
        if ($linkids) {
            Db::name('linkman_method')->where("id in($linkids)")->delete();//删除联系人的方式表
        }
        Db::name('linkman')->where("co_id in($co_id)")->delete();//删除公司联系人表
        Db::name('co_bank')->where("co_id in($co_id)")->delete();//删除公司银行表
    }
}



